import axios from 'axios'

interface QuestionState {
	awserList: any[],
	list: ListItemState[]
}

export interface ListItemState {
	title: string,
	id: string,
	url: string,
	question: ListItemQuestionState[]
}

export interface ListItemQuestionState {
	'type': '单选' | '多选'
	"title": string,
	"selects": string[]
}

const initState: QuestionState = {
	awserList: [],
	list: []
}


const SET_LIST = 'SET_LIST';

export function setList(payload: ListItemState[]): SetListType {
	return { type:  SET_LIST, payload}
}

export function fetchList() {
	return async (dispatch: any) => {
		const resp = await axios.get('/api/list');
		dispatch(setList(resp.data.data as ListItemState[]))
	}
}

interface SetListType {
	type: typeof SET_LIST
	payload: ListItemState[]
}

type ActionTypes = SetListType


const questionReducer = (state = initState, action: ActionTypes) => {
	switch (action.type) {
		case 'SET_LIST': 
			return {
				...state,
				list: action.payload
			}
		default:
			return state
	}
}


export default questionReducer